#include "LinkExercise.h"

void add(struct node**s, string tosave)
{
    struct node *temp, *r;
    temp = new node;
    temp->input = tosave;
    temp->next = NULL;
    if(*s==NULL)
    {
        *s=temp;
    }
    else
    {
        r=*s;
        while(r->next!=NULL)
        {
            r=r->next;
        }
        r->next=temp;
    }
}

void display(struct node*s)
{
    struct node*ptr;
    ptr=s;
    cout << "Display: "<< endl;
    while (ptr!=NULL)
    {
        cout << ptr->input << endl;
        ptr= ptr->next;
    }
}

void search(struct node*s, string tosearch)
{
    struct node*tempo;
    tempo = s;
    while (tempo !=NULL)
    {
        if (tosearch == tempo->input)
        {
            cout << "Yes it Exists" << endl;
            break;
        }
        else
        {
            tempo = tempo->next;
        }
     cout << "Search Turned up Invalid" << endl;
    }
   
}


int main()
{
    struct node *test = NULL;
    int x;
    string data; 
    while (1)
    {
    cout << "Welcome To The Program" << endl;
    cout << "Please Choose a Number to the corresponding Function" << endl;
    cout << "0 - Exit the Program" << endl;
    cout << "1 - Save A String" << endl;
    cout << "2 - Output All Saved Strings" << endl;
    cout << "3 - Search if a String Exists" << endl;
    cin >> x;
    switch(x)
    {
        case 0:
            return 0;
            break;
        case 1: 
            cout<< "Enter the String You Want to Save" << endl;
            cin >> data; 
            add(&test,data);
            cout << "Saved" << endl;
            break;
        case 2:
            display(test);
            break;
        case 3:
            cout<< "Enter the String You Want to Search" << endl;
            cin >> data; 
            search(test,data);
            break;
        default:
            cout << "Input a Valid Number" << endl;
            break;
        }
    }
}
